import java.util.*;

public class Solution1235 {
    public int jobScheduling(int[] startTime, int[] endTime, int[] profit) {
        TreeSet<Integer> treeSet=new TreeSet<>();
        for (int i : startTime) {
            treeSet.add(i);
        }
        for (int i : endTime) {
            treeSet.add(i);
        }
        Map<Integer,Integer> map=new HashMap<>();
        int count=0;
        for (Integer integer : treeSet) {
            count++;
            map.put(integer,count);
        }
        List<int[]> list=new ArrayList<>();
        for (int i=0;i<startTime.length;i++){
            list.add(new int[]{map.get(startTime[i]),map.get(endTime[i]),profit[i]});
        }
        list.sort(Comparator.comparingInt(a -> a[1]));
        int[] f=new int[count+1];
        int t=0;
        for (int i=1;i<f.length;i++){
            f[i]=f[i-1];
            while (t<list.size() && list.get(t)[1]==i){
                f[i]=Math.max(f[i],f[list.get(t)[0]]+list.get(t)[2]);
                t++;
            }
        }
        return f[count];
    }

    public static void main(String[] args) {
        System.out.println(new Solution1235().jobScheduling(new int[]{1,2,3,3},new int[]{3,4,5,6},new int[]{50,10,40,70}));
    }
}
